<template>
  <div class="container">
    <van-nav-bar left-arrow @click-left="$router.go(-1)" />
    <div class="inner">
      <button class="area" @click="$router.push('/city')">
        {{ currentcity.label }}<van-icon name="arrow-down" size="8" />
      </button>
      <van-search
        v-model="value"
        disabled
        show-action
        placeholder="请输入小区或地址"
      >
        <template #action>
          <div @click="onSearch">
            <van-icon name="location-o" size="22px" color="#fff" />
          </div>
        </template>
      </van-search>
    </div>
    <van-sticky :offset-top="0">
      <div class="nav">
        <div
          class="nav-item"
          :class="{ 'nav-item-active': change === area }"
          @click="dealpop(area)"
        >
          区域
        </div>
        <div
          class="nav-item"
          :class="{ 'nav-item-active': change === rentType }"
          @click="dealpop(rentType)"
        >
          方式
        </div>
        <div
          class="nav-item"
          :class="{ 'nav-item-active': change === price }"
          @click="dealpop(price)"
        >
          租金
        </div>
        <div
          class="nav-item"
          :class="{ 'nav-item-active': flag }"
          @click="dealfilter"
        >
          筛选
        </div>
      </div>
    </van-sticky>

    <div class="pop" :style="{ display: show }">
      <div class="pop-contain">
        <div class="nav">
          <div
            class="nav-item"
            :class="{ 'nav-item-active': change === area }"
            @click="change = area"
          >
            区域
          </div>
          <div
            class="nav-item"
            :class="{ 'nav-item-active': change === rentType }"
            @click="change = rentType"
          >
            方式
          </div>
          <div
            class="nav-item"
            :class="{ 'nav-item-active': flag }"
            @click="change = price"
          >
            租金
          </div>
          <div
            class="nav-item"
            :class="{ 'nav-item-active': change === 'filter' }"
            @click="dealfilter"
          >
            筛选
            <!-- <van-cell is-link @click="showPopup">展示弹出层</van-cell> -->
          </div>
        </div>
        <van-picker :item-height="34" show-toolbar :columns="change" />
        <div class="pop-bottom">
          <div class="button-back" @click="dealback">取消</div>
          <div class="button-confirm" @click="dealconfirm">确定</div>
        </div>
      </div>
    </div>
    <van-popup
      v-model="show1"
      position="right"
      :style="{ height: '100%', width: '295px' }"
    >
      <div class="pop-right">
        <dl>
          <dt>户型</dt>
          <dd>
            <span v-for="(item, index) in roomType" :key="index">{{
              item.text
            }}</span>
          </dd>
          <dt>朝向</dt>
          <dd>
            <span v-for="(item, index) in oriented" :key="index">{{
              item.text
            }}</span>
          </dd>
          <dt>楼层</dt>
          <dd>
            <span v-for="(item, index) in floor" :key="index">{{
              item.text
            }}</span>
          </dd>
          <dt>房屋亮点</dt>
          <dd>
            <span v-for="(item, index) in characteristic" :key="index">{{
              item.text
            }}</span>
          </dd>
        </dl>
        <div class="bottom">
          <div class="button-back" @click="dealback">清除</div>
          <div class="button-confirm" @click="dealconfirm">确定</div>
        </div>
      </div>
    </van-popup>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-card
        v-for="(item, index) in allhouselist"
        :key="index"
        :price="item.price"
        :desc="item.desc"
        :title="item.title"
        :thumb="`http://liufusong.top:8080` + item.houseImg"
      >
        <template #tags>
          <van-tag v-for="(item, index) in item.tags" :key="index">{{
            item
          }}</van-tag>
        </template>
      </van-card>
    </van-list>
  </div>
</template>

<script>
import { getcondition, getallhouse } from '@/api/house'
export default {
  name: 'findhouse-page',
  async created() {
    const res = await getcondition({ id: this.currentcity.value })
    function deepClone(no, oo) {
      for (const k in oo) {
        if (oo[k] instanceof Array) {
          no[k] = []
          deepClone(no[k], oo[k])
        } else if (oo[k] instanceof Object) {
          no[k] = {}
          deepClone(no[k], oo[k])
        } else {
          if (k === 'label') {
            no.text = oo[k]
          } else {
            no[k] = oo[k]
          }
        }
      }
    }
    const result = {}
    deepClone(result, res.body)
    this.ce = result
    result.area.children[0].children = []
    this.area.push(result.area, result.subway)
    this.rentType = result.rentType
    this.price = result.price
    this.floor = result.floor
    this.oriented = result.oriented
    this.roomType = result.roomType
    this.characteristic = result.characteristic
  },
  components: {},
  props: {},
  data() {
    return {
      value: '',
      currentcity: JSON.parse(localStorage.getItem('currentcity')) || {
        label: '北京',
        value: 'AREA|88cff55c-aaa4-e2e0',
        pinyin: 'beijing',
        short: 'bj'
      },
      flag: false,
      show: 'none',
      show1: false,
      filter: [],
      change: [],
      rentType: [],
      area: [],
      price: [],
      floor: [],
      oriented: [],
      roomType: [],
      characteristic: [],
      allhouselist: [],
      loading: false,
      finished: false,
      start: 1
    }
  },
  computed: {},
  watch: {},
  methods: {
    onSearch(val) {
      console.log(val)
      this.$router.push('/map')
    },
    dealback() {
      this.show = 'none'
    },
    dealconfirm() {
      this.show = 'none'
    },
    dealpop(data) {
      this.show = 'block'
      this.change = data
      this.flag = false
    },
    dealfilter() {
      this.show = 'none'
      this.show1 = true
      this.flag = true
    },
    showPopup() {
      this.show1 = true
    },
    async onLoad() {
      const res1 = await getallhouse({
        id: this.currentcity.value,
        start: this.start,
        end: this.start + 20
      })
      this.allhouselist.push(...res1.body.list)
      // 加载状态结束
      this.loading = false
      this.start += 20

      // 数据全部加载完成
      if (this.start > 106456) {
        this.finished = true
      }
    }
  }
}
</script>

<style scoped lang="less">
.container {
  position: relative;
}
/deep/.van-nav-bar__left {
  top: 8px;
  left: -5px;
}
/deep/.van-nav-bar {
  height: 50px;
}
.area {
  height: 34px;
  width: 64px;
  text-align: center;
  position: absolute;
  font-size: 14px;
  top: 26px;
  left: 20px;
  background-color: #fff;
  &::after {
    content: '';
    width: 2px;
    height: 18px;
    background-color: #ccc;
    position: absolute;
    transform: translate(8px, 1px);
  }
}
.van-search {
  width: 290px;
  height: 36px;
  position: absolute;
  top: 25px;
  left: 70px;
  background: rgba(0, 0, 0, 0);
}
/deep/.van-search__content {
  background-color: #fff;
}
.inner {
  position: absolute;
  width: 340px;
  height: 50px;
  z-index: 3;
  left: 15px;
  top: -17px;
  border-radius: 5px;
}
.nav {
  display: flex;
  width: 100%;
  height: 40px;
  background: #fff;
  border: 1px solid #ccc;
}
.nav-item {
  width: 25%;
  padding-left: 20px;
  font-size: 17px;
  height: 40px;
  line-height: 40px;
  cursor: pointer;
  position: relative;
}
.nav-item::before {
  content: '';
  position: absolute;
  top: 15px;
  right: 25px;
  width: 0;
  height: 0;
  border-top: 6px solid #ccc;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
}
.nav-item-active {
  color: rgb(33, 185, 122);
}
.nav-item-active::before {
  border-top: 6px solid rgb(33, 185, 122);
}
.pop {
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  position: fixed;
  z-index: 100;
  background-color: rgba(0, 0, 0, 0.3);
  .pop-contain {
    position: absolute;
    top: 50px;
    left: 0px;
    width: 100%;
    height: 350px;
    background-color: #fff;
    .pop-bottom {
      position: absolute;
      bottom: 0px;
      left: 0px;
      width: 100%;
      height: 50px;
      border: 1px solid #ccc;
      line-height: 50px;
      display: flex;
      font-size: 18px;
      .button-back {
        width: 125px;
        text-align: center;
      }
      .button-confirm {
        width: 250px;
        text-align: center;
        background-color: rgb(33, 185, 122);
        color: #fff;
      }
    }
  }
}
.van-card {
  width: 345px;
  height: 120px;
  margin: 0 15px;
  background-color: #fff;
  border-bottom: 1px solid #ccc;
  padding: 18px 0px 0px 0px;
  /deep/.van-card__title {
    font-size: 15px;
    color: #394043;
    font-weight: bold;
    height: 22px;
    line-height: 22px;
  }
  /deep/.van-card__desc {
    font-size: 12px;
    color: #afb2b3;
    height: 22px;
    line-height: 22px;
  }
  /deep/.van-tag {
    color: #39becd;
    background: #e1f5f8;
    display: inline-block;
    font-size: 12px;
    border-radius: 3px;
    padding: 4px 5px;
    margin-right: 5px;
    line-height: 12px;
  }
  /deep/.van-card__price-currency {
    display: none;
  }
  /deep/.van-card__price-integer {
    font-size: 16px;
    color: #fa5741;
    font-weight: bold;
    &::after {
      content: ' 元/月';
      font-size: 12px;
      font-weight: normal;
    }
  }
  /deep/.van-card__thumb {
    width: 106px;
    height: 80px;
    img {
      border-radius: 0px;
    }
  }
}
.van-picker {
  /deep/.van-picker__toolbar {
    display: none;
  }
  z-index: 1;
  height: 260px;
}
.pop-right {
  position: relative;
  height: 667px;
  width: 100%;
  overflow: auto;
  dl {
    padding: 14px 15px;
    dt {
      margin: 20px 0;
      font-size: 15px;
    }
    dd {
      border-bottom: 1px solid #ccc;
      padding: 0px 0px 14px 40px;
      span {
        display: inline-block;
        height: 32px;
        width: 70px;
        margin: 0 18px 15px 0;
        border: 1px solid #ddd;
        border-radius: 3px;
        line-height: 32px;
        text-align: center;
        font-size: 12px;
        color: #888;
      }
    }
  }
  .bottom {
    position: fixed;
    z-index: 2;
    bottom: 0;
    left: 0px;
    width: 100%;
    height: 50px;
    text-align: center;
    line-height: 50px;
    border: 1px solid #ccc;
    display: flex;
    font-size: 18px;
    .button-back {
      width: 100px;
      background-color: #fff;
    }
    .button-confirm {
      width: 200px;
      background-color: rgb(33, 185, 122);
      color: #fff;
    }
  }
}
.filter-active {
  border: 1px solid #21b97a;
  color: #21b97a;
  background-color: #defaef;
}
</style>
